package com.sale.plugins;

import com.apache.api.manager.ProxyManager;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.common.util.TransTimeUtil;
import com.apache.database.constant.SpringContextLoader;
import com.apache.exception.BusinessException;
import com.apache.ius.plugin.CustomMethodPlugin;
import com.apache.ius.plugin.IusPluginUtil;
import com.apache.tools.DataMap;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 发售交易-委托交易时间判断：
 *
 * @author admin
 */
public class SaleOrderTimeCheckPluginImpl implements CustomMethodPlugin {

    private Logger log = Logger.getLogger(getClass());

    @Override
    public Object beforeInvoke(Map<String, Object> param) {
        String proId = String.valueOf(param.get("trsOrder.proId"));
        String proNo = String.valueOf(param.get("trsOrder.proNo"));
        String proName = String.valueOf(param.get("trsOrder.proName"));

        List<DataMap> rulesListMap = getTransRulesList(param, proId);
        if (rulesListMap == null || rulesListMap.size() < 1) {
            log.info("无时间规则校验");
            return null;
        }
        Map<String, Boolean> timeCheckMap = new HashMap<String, Boolean>();
        String timeStr = "";
        for (DataMap buyMap : rulesListMap) {
            String paramVal = String.valueOf(buyMap.get("paramNo"));
            String rulesNo = String.valueOf(buyMap.get("rulesNo"));

            //上午交易时间验证
            String startStr = paramVal;
            String start = startStr.split("-")[0];
            String end = startStr.split("-")[1];
            boolean timeFlag = TransTimeUtil.checkTransTime(start + ":00", end + ":00");
            log.info("[" + proNo + "][" + proName + "]交易时间范围检测【" + startStr + "】=" + timeFlag);
            timeStr += startStr + ",";
            timeCheckMap.put(rulesNo, timeFlag);
        }

        int falseCount = 0;
        if (timeCheckMap != null && timeCheckMap.size() > 0) {
            for (String key : timeCheckMap.keySet()) {
                boolean falgTime = timeCheckMap.get(key);
                if (!falgTime) {
                    falseCount++;
                }
            }
        }
        //falseCount>0并且验证失败的个数全等于验证规则个数（就是所有规则都没验证通过的情况下就需要给用户提示）
        if (falseCount > 0 && falseCount == timeCheckMap.size()) {
            if (timeStr.length() > 0)
                timeStr = timeStr.substring(0, timeStr.length() - 1);
            throw new BusinessException("不允许委托下单,\n<br/>交易时间[" + timeStr + "]");
        }

        return null;
    }

    @Override
    public Object doInvoke(Map<String, Object> param) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Object afterInvoke(Map<String, Object> param) {
        // TODO Auto-generated method stub
        return null;
    }

    /**
     * 查询商品交易规则
     *
     * @param param
     * @param proId
     * @return
     */
    @SuppressWarnings("rawtypes")
    public List<DataMap> getTransRulesList(Map<String, Object> param, String proId) {
//        ProxyManager netProxyManager = (ProxyManager) SpringContextLoader
//                .getBean("netProxyManager");
//        ParamsVo params = new ParamsVo();
//        params.setParams("doCode", "dymicSql");
//        params.setParams("sqlKeyId", "ByDymicSql");
//        params.setParams("resultType", "objInfo");
//        params.setKey("publicForSql");

        Map<String, Object> whereMap = new HashMap<String,Object>();
        //封装参数
         whereMap.put("trsRulesPro.w_showFlag", "T");
         whereMap.put("trsRulesPro.w_rulesType", "guadan");
         whereMap.put("trsRulesPro.w_constantType", "order_status");
         whereMap.put("trsRulesPro.w_dataType", "datetext");
         whereMap.put("trsRulesPro.w_prodId", proId);
         whereMap.put("trsRulesPro.wl_rulesNo", "date");
         whereMap.put("orderBy", "trsRulesPro.rules_no asc");

        ResultEntity resultEntity1 = IusPluginUtil.getInstance().select("list","s_trsRulesPro",whereMap);
        if(resultEntity1==null || resultEntity1.getEntity()==null){
            throw new BusinessException("查询规则失败！");
        }
        List<DataMap> list = (List<DataMap>)resultEntity1.getEntity();
        if(list==null || list.size()==0){
            throw new BusinessException("查询规则失败！");
        }
        return list;
    }

    /**
     * 规则校验入口（专门给系统公式规则提供的）
     *
     * @param paramsVo
     */
    public void run(ParamsVo paramsVo) {
        beforeInvoke(paramsVo.getParams());
    }
}
